package com.dearay.noweibo.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
	private static final long	serialVersionUID	= -6417782063917589670L;

	@Override
	public String execute() throws Exception
	{
		UserService userService = UserServiceFactory.getUserService();         
		User user = userService.getCurrentUser();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		//PrintWriter out = response.getWriter();
		if (user != null) {
			//response.sendRedirect(request.getRequestURI());
			return SUCCESS;
		} else {
			response.sendRedirect(userService.createLoginURL(request.getRequestURI()));         
		}
		return SUCCESS;
	}
}